import { defineConfig, loadEnv, ConfigEnv } from 'vite';

//
import { getPlugins } from './config/vite.plugins';
import { devServer } from './config/dev.server';
import { getBuild } from './config/vite.build';

//
import { getLessOptions } from './config/less.config';

const root = process.cwd();

// https://vitejs.dev/config/
export default ({ mode, command }: ConfigEnv) => {
  const env = loadEnv(mode, root);
  const host = env.VITE_HOST || '127.0.0.1';
  const port = +env.VITE_PORT || 3000;
  const publicUrl = env.VITE_PUBLIC_URL ?? './';

  return defineConfig({
    mode,
    css: {
      preprocessorOptions: {
        less: getLessOptions({ mode, command }),
      },
    },
    // 配置别名
    alias: {
      '~': '/',
      '@/': '/src/',
    },
    plugins: [
      ...getPlugins({
        publicUrl,
        command,
        mode
      }),
    ],
    base: publicUrl,
    // 服务
    server: devServer({ host, port }),
    // 配置别名
    build: {
      outDir: 'dist',
      manifest: true,
      assetsDir: 'static',
      ...getBuild(mode),
    },
    // 引入第三方的配置
    optimizeDeps: {
      include: [],
    },
  });
};
